﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace NetworkDemo
{
     public class GameplayScreen : Screen
     {
         int framesSinceLastSend = 0;
         int framesBetweenPackets = 10;

         public override void HandleInput(InputState input)
         {
             if (input.IsKeyDown(Keys.Down))
             {
                 ScreenManager.GameController.MoveClientSprite(Keys.Down);
             }
             if (input.IsKeyDown(Keys.Up))
             {
                 ScreenManager.GameController.MoveClientSprite(Keys.Up);
             }
             if(input.IsKeyDown(Keys.Left))
             {
                 ScreenManager.GameController.MoveClientSprite(Keys.Left);
             }
             if(input.IsKeyDown(Keys.Right))
             {
                 ScreenManager.GameController.MoveClientSprite(Keys.Right);
             }
         }
         #region Update
         public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
         {
             base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
             framesSinceLastSend++;

             if (framesSinceLastSend >= framesBetweenPackets)
             {
                 framesSinceLastSend = 0;
                 ScreenManager.GameController.PushNetworkUpdate();
             }
         }
         #endregion
         #region Draw
         /// <summary>
        /// Draw uses a layer-based approach, where the layers are drawn in ascending order.
        /// </summary>
        /// <param name="gameTime"></param>
        
        
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.None);
            DrawSprites(gameTime, spriteBatch, ScreenManager.GameModel);
            DrawBackground(spriteBatch, ScreenManager.GameModel);
            spriteBatch.End();
        }

        public void DrawSprites(GameTime gameTime, SpriteBatch spriteBatch, Game1 model)
        {
            List<Player> player = model.PlayerManager.GetAllPlayers();
            foreach (Player p in player)
            {
                p.Draw(gameTime, spriteBatch, ScreenManager.Font);
            }
        }
        public void DrawBackground(SpriteBatch spriteBatch, Game1 model)
        {
            Texture2D grassTexture = model.Content.Load<Texture2D>("grass2"); // temp
            spriteBatch.Draw(grassTexture, new Rectangle(0, 0, ScreenManager.GraphicsDevice.DisplayMode.Width, ScreenManager.GraphicsDevice.DisplayMode.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, (float)Layer.Ground / 100.0f); 
        }
        #endregion
    }
}
